/*
 * FeedDateComparator.java
 * 
 * Copyright (C) 2009 no.good.at.coding
 * 
 * This file is part of the No Good Commons Library.
 *
 * The No Good Commons Library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nogoodatcoding.commons;

import java.util.Comparator;
import com.sun.syndication.feed.synd.SyndEntryImpl;

import org.apache.log4j.*;

/**
 *
 * A comparator for {@link com.sun.syndication.feed.synd.SyndEntry} objects that
 * compares them on basis of {@code publishedDate}.
 *
 * @author no.good.at.coding
 *
 */

public class FeedDateComparator
        implements Comparator
{
    private static Logger log = Logger.getLogger(FeedDateComparator.class);

    private boolean isDescending = true;

    /**
     *
     * Default constructor for convenience; {@code sortDescending} is assumed to
     * be {@code true}. Will return a comparator that will sort by descending
     * order of timestamps i.e. newest entries first
     *
     * @see FeedDateComparator#FeedDateComparator(boolean)
     *
     */
    public FeedDateComparator()
    {
        this.isDescending = true;
    }

    /**
     *
     * Constructor for {@code FeedDateComparator}.
     *
     * @param sortDescending Indicates if the sorting is to be in descending
     * order (newest first) or not (i.e. in descending order (oldest first)).
     *
     * @see com.nogoodatcoding.commons.FeedDateComparator#FeedDateComparator()
     *
     */
    public FeedDateComparator(boolean sortDescending)
    {
        this.isDescending = sortDescending;
    }

    public int compare(Object o1, Object o2)
    {

        log.debug("Comparing item1 and item2");

        if ( o1 == null | o2 == null )
        {
            log.error("Either item1 or item2 was null");
            throw new NullPointerException();
        }

        String item1 = ((SyndEntryImpl)o1).getTitle();
        String item2 = ((SyndEntryImpl)o2).getTitle();

        long time1 = ((SyndEntryImpl)o1).getPublishedDate().getTime();
        long time2 = ((SyndEntryImpl)o2).getPublishedDate().getTime();

        if ( time1 < time2 )
        {
            if(isDescending)
            {
                log.debug(item1 + " < " + item2);
                return 1;
            }
            else
            {
                log.debug(item1 + " > " + item2);
                return -1;
            }
        }
        else if ( time1 > time2 )
        {
            if(isDescending)
            {
                log.debug(item1 + " > " + item2);
                return -1;
            }
            else
            {
                log.debug(item1 + " < " + item2);
                return 1;
            }
        }
        else
        {
            log.debug(item1 + " == " + item2);
            return 0;
        }
    }
}